package com.sx.pipeline.ui.forget

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.SmsCodeType
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActForgetPwdBinding

/**
 * @Author: YY
 * @Date: 2022/4/14
 * @Description: 忘记密码
 */
@Route(path = RoutePath.ForgetPwd.FORGET_PWD)
class ForgetPwdActivity: CYDataBindingBaseActivity<ActForgetPwdBinding, CYBaseViewModel>
    (R.layout.act_forget_pwd), View.OnClickListener {

    override fun initStatusBar() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(false).init()
    }

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
    }

    override fun initViewObservable() {
        super.initViewObservable()

        observe(mViewModel.smsCodeEvent) {
            mBinding.tvCountDown.start()
        }

        observe(mViewModel.modifyEvent) {
            finish()
        }
    }

    override fun onClick(v: View) {
        when(v.id) {
            R.id.image_left_gray -> {
                finish()
            }
            R.id.tv_count_down -> {
                val phone = mBinding.etPhone.text.toString()
                mViewModel.sendSmsCode(phone, SmsCodeType.FORGOT_PWD_SMS.code)
            }
            R.id.tv_confirm -> {
                val phone = mBinding.etPhone.text.toString()
                val smsCode = mBinding.etVerifyCode.text.toString()
                val pwd = mBinding.etPwd.text.toString()
                val confirmPwd = mBinding.etConfirmPwd.text.toString()
                if (confirmPwd != pwd) {
                    ToastUtils.showShort("两次密码输入不一致，请重新输入")
                    return
                }
                mViewModel.modifyPwd(0, phone, smsCode, confirmPwd)
            }
        }
    }
}